<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="45%" v-dialogDrag append-to-body>
      <el-form ref="formRef" :model="formData" :rules="formRules" v-loading="formLoading" label-width="100px">
                    <el-form-item label="房屋的名称" prop="houseId">
                      <el-select v-model="formData.houseId" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.houseName"
              :value="item.id"
            >
            </el-option>
          </el-select>
                    </el-form-item>
                    <el-form-item label="标签名称" prop="name">
                      <el-input v-model="formData.name" placeholder="请输入标签名称" />
                    </el-form-item>
      </el-form>
              <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="formLoading">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as HouseInfoApi from '@/api/system/houseinfo';
  import * as TagesApi from '@/api/system/tages';
      export default {
    name: "TagesForm",
    components: {
                    },
    data() {
      return {
        queryParams:{ offset: 1,
          size: 10,},
        options: {},
        // 弹出层标题
        dialogTitle: "",
        // 是否显示弹出层
        dialogVisible: false,
        // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
        formLoading: false,
        // 表单参数
        formData: {
                            id: undefined,
                            houseId: undefined,
                            name: undefined,
        },
        // 表单校验
        formRules: {
                        houseId: [{ required: true, message: '房屋的id不能为空', trigger: 'blur' }],
        },
                        };
    },
    created() {
      this.getListone();
    },
    methods: {
      async getListone() {
      try {
      this.loading = true;
         const res = await HouseInfoApi.getHouseInfoPage(this.queryParams);
        this.options = res.data.records;
    
      } finally {
        this.loading = false;
      }
    },
      /** 打开弹窗 */
     async open(id) {
        this.dialogVisible = true;
        this.reset();
        // 修改时，设置数据
        if (id) {
          this.formLoading = true;
          try {
            const res = await TagesApi.getTages(id);
            this.formData = res.data;
            this.title = "修改标签";
          } finally {
            this.formLoading = false;
          }
        }
        this.title = "新增标签";
              },
      /** 提交按钮 */
      async submitForm() {
        // 校验主表
        await this.$refs["formRef"].validate();
                  this.formLoading = true;
        try {
          const data = this.formData;
                  // 修改的提交
          if (data.id) {
            await TagesApi.updateTages(data);
            this.$modal.msgSuccess("修改成功");
            this.dialogVisible = false;
            this.$emit('success');
            return;
          }
          // 添加的提交
          await TagesApi.createTages(data);
          this.$modal.msgSuccess("新增成功");
          this.dialogVisible = false;
          this.$emit('success');
        } finally {
          this.formLoading = false;
        }
      },
                      /** 表单重置 */
      reset() {
        this.formData = {
                            id: undefined,
                            houseId: undefined,
                            name: undefined,
        };
        this.resetForm("formRef");
      }
    }
  };
</script>
